package com.qf.flash.mvp.presenter;

import com.qf.flash.http.HttpApi;
import com.qf.flash.http.HttpManager;
import com.qf.flash.http.response.AreaMsgBean;
import com.qf.flash.http.response.BannerBean;
import com.qf.flash.http.response.PlayTypeBean;
import com.qf.flash.model.HomeTopBean;
import com.qf.flash.mvp.contract.HomeContract;
import com.qf.flashlib.base.BasePresenter;
import com.qf.flashlib.base.BaseSubscriber;
import com.qf.flashlib.util.RxUtils;

import java.util.List;

import io.reactivex.Observable;

/**
 * 创建日期：2020/3/9 on 10:28
 * 描述:
 * 作者:戴旭东
 */
public class BannerPresenter extends BasePresenter<HomeContract.View> implements HomeContract.Presenter {


    @Override
    public void getBannerPlayType() {
        HttpApi api = HttpManager.getInstance().getGsonHttpApi();

        Observable<List<BannerBean>> banner = api.BANNER_LIST()
                .compose(RxUtils.<List<BannerBean>>handleResult())
                .compose(RxUtils.<List<BannerBean>>io_main());
        Observable<List<PlayTypeBean>> playType = api.PLAY_TYPES()
                .compose(RxUtils.<List<PlayTypeBean>>handleResult())
                .compose(RxUtils.<List<PlayTypeBean>>io_main());
        addSubscription(
                Observable.zip(banner,
                        playType, (bannerBeans, playTypeBeans) -> {
                            HomeTopBean homeTopBean = new HomeTopBean();
                            homeTopBean.setBannerBeans(bannerBeans);
                            homeTopBean.setPlayTypeBeans(playTypeBeans);
                            return homeTopBean;
                        }).compose(RxUtils.io_main())
                        .subscribeWith(new BaseSubscriber<HomeTopBean>(mRootView) {
                            @Override
                            public void onSuccess(HomeTopBean homeTopBean) {
                                mRootView.setBannerPlayTypeData(homeTopBean);
                            }
                        })
        );
    }

    @Override
    public void getAreaId(String adCode) {
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .AREA_MSG_BY_CODE(adCode)
                .compose(RxUtils.handleResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<AreaMsgBean>(mRootView) {
                    @Override
                    public void onSuccess(AreaMsgBean areaMsgBean) {
                        mRootView.setAreaId(areaMsgBean);
                    }
                })
        );
    }
}
